home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / UpdateManager / GtkProgress.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  4.8 KB  |  129 lines

  1. # GtkProgress.py 
  2. #  
  3. #  Copyright (c) 2004,2005 Canonical
  4. #  
  5. #  Author: Michael Vogt <michael.vogt@ubuntu.com>
  6. #  This program is free software; you can redistribute it and/or 
  7. #  modify it under the terms of the GNU General Public License as 
  8. #  published by the Free Software Foundation; either version 2 of the
  9. #  License, or (at your option) any later version.
  10. #  This program is distributed in the hope that it will be useful,
  11. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #  GNU General Public License for more details.
  14. #  You should have received a copy of the GNU General Public License
  15. #  along with this program; if not, write to the Free Software
  16. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. #  USA
  18.  
  19. import pygtk
  20. pygtk.require('2.0')
  21. import gtk
  22. import apt
  23. import apt_pkg
  24. from gettext import gettext as _
  25. from Core.utils import *
  26.  
  27. # intervals of the start up progress
  28. # 3x caching and menu creation
  29. STEPS_UPDATE_CACHE = [33, 66, 100]
  30.  
  31. class GtkOpProgress(apt.OpProgress):
  32.     def __init__(self, host_window, progressbar, status, parent,
  33.                  steps=STEPS_UPDATE_CACHE):
  34.         # used for the "one run progressbar"
  35.         self.steps = steps[:]
  36.         self.base = 0
  37.         self.old = 0
  38.         self.next = int(self.steps.pop(0))
  39.  
  40.         self._parent = parent
  41.         self._window = host_window
  42.         self._status = status
  43.         self._progressbar = progressbar
  44.         # Do not show the close button 
  45.         self._window.realize()
  46.         host_window.window.set_functions(gtk.gdk.FUNC_MOVE)
  47.         self._window.set_transient_for(parent)
  48.  
  49.     def update(self, percent):
  50.         #print percent
  51.         #print self.Op
  52.         #print self.SubOp
  53.         # only show progress bar if the parent is not iconified (#353195)
  54.         state = self._parent.window.get_state()
  55.         if not (state  & gtk.gdk.WINDOW_STATE_ICONIFIED):
  56.             self._window.show()
  57.         self._parent.set_sensitive(False)
  58.         # if the old percent was higher, a new progress was started
  59.         if self.old > percent:
  60.             # set the borders to the next interval
  61.             self.base = self.next
  62.             try:
  63.                 self.next = int(self.steps.pop(0))
  64.             except:
  65.                 pass
  66.         progress = self.base + percent/100 * (self.next - self.base)
  67.         self.old = percent
  68.         self._status.set_markup("<i>%s</i>" % self.op)
  69.         self._progressbar.set_fraction(progress/100.0)
  70.         while gtk.events_pending():
  71.             gtk.main_iteration()
  72.  
  73.     def done(self):
  74.         self._parent.set_sensitive(True)
  75.     def hide(self):
  76.         self._window.hide()
  77.  
  78. class GtkFetchProgress(apt.progress.FetchProgress):
  79.     def __init__(self, parent, summary="", descr=""):
  80.         # if this is set to false the download will cancel
  81.         self._continue = True
  82.         # init vars here
  83.         # FIXME: find a more elegant way, this sucks
  84.         self.summary = parent.label_fetch_summary
  85.         self.status = parent.label_fetch_status
  86.         self.progress = parent.progressbar_fetch
  87.         self.window_fetch = parent.window_fetch
  88.         self.window_fetch.set_transient_for(parent.window_main)
  89.         self.window_fetch.realize()
  90.         self.window_fetch.window.set_functions(gtk.gdk.FUNC_MOVE)
  91.         # set summary
  92.         if self.summary != "":
  93.             self.summary.set_markup("<big><b>%s</b></big> \n\n%s" %
  94.                                     (summary, descr))
  95.     def start(self):
  96.         self.progress.set_fraction(0)
  97.         self.window_fetch.show()
  98.     def stop(self):
  99.         self.window_fetch.hide()
  100.     def on_button_fetch_cancel_clicked(self, widget):
  101.         self._continue = False
  102.     def pulse(self):
  103.         apt.progress.FetchProgress.pulse(self)
  104.         currentItem = self.currentItems + 1
  105.         if currentItem > self.totalItems:
  106.           currentItem = self.totalItems
  107.         if self.currentCPS > 0:
  108.             statusText = (_("Downloading file %(current)li of %(total)li with "
  109.                             "%(speed)s/s") % {"current" : currentItem,
  110.                                               "total" : self.totalItems,
  111.                                               "speed" : humanize_size(self.currentCPS)})
  112.         else:
  113.             statusText = (_("Downloading file %(current)li of %(total)li") % \
  114.                           {"current" : currentItem,
  115.                            "total" : self.totalItems })
  116.             self.progress.set_fraction(self.percent/100.0)
  117.         self.status.set_markup("<i>%s</i>" % statusText)
  118.         # TRANSLATORS: show the remaining time in a progress bar:
  119.         #self.progress.set_text(_("About %s left" % (apt_pkg.TimeToStr(self.eta))))
  120.     # FIXME: show remaining time
  121.         self.progress.set_text("")
  122.  
  123.         while gtk.events_pending():
  124.             gtk.main_iteration()
  125.         return self._continue
  126.